package com.wswy.wzcx.ui.adapter;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.wswy.wzcx.ui.hold.ViewHolder;

import java.util.List;

/**
 * Created by wangduheng26 on 2018/3/6.
 * wangduheng26@gmail.com
 */

public abstract class BaseTypeAdapter<T> extends RecyclerView.Adapter<ViewHolder> {
    public static final int ITEM_TYPE_00 = 0;
    public static final int ITEM_TYPE_01 = 1;
    public static final int ITEM_TYPE_02 = 2;
    private List<T> list;
    protected LayoutInflater inflater;

    public BaseTypeAdapter(List<T> list) {
        this.list = list;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        inflater = LayoutInflater.from(parent.getContext());
        if (viewType == ITEM_TYPE_00) {
            return onCreateViewHolder01(parent);
        } else if (viewType == ITEM_TYPE_01) {
            return onCreateViewHolder02(parent);
        } else if (viewType == ITEM_TYPE_02) {
            return onCreateViewHolder03(parent);

        }
        return null;
    }


    public abstract ViewHolder onCreateViewHolder02(ViewGroup parent);

    public abstract ViewHolder onCreateViewHolder01(ViewGroup parent);

    public abstract void onDataBinded02(ViewHolder holder, T item);


    public abstract void onDataBinded01(ViewHolder holder, T item);


    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        T item = getItem(position);
        if (item != null && holder != null) {
            holder.bindData(item);
            int viewType = getItemViewType(position);
            if (viewType == ITEM_TYPE_00) {
                onDataBinded01(holder, item);
            } else if (viewType == ITEM_TYPE_01) {
                onDataBinded02(holder, item);
            } else if (viewType == ITEM_TYPE_02) {
                onDataBinded03(holder, item);
            }
        }

    }


    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public T getItem(int position) {
        try {
            return list.get(position);
        } catch (Exception e) {
            Log.e("wzcx", "getItem", e);
            return null;
        }
    }

    protected void onDataBinded03(ViewHolder holder, T item) {

    }

    protected ViewHolder onCreateViewHolder03(ViewGroup parent) {
        return null;
    }
}
